package org.infosec.lc.exercises.ObjectNetworkTransmission;

import org.infosec.lc.exercises.ObjectNetworkTransmission.code.castor.CastorUDPReceive;
import org.infosec.lc.exercises.ObjectNetworkTransmission.code.castor.CastorUDPSend;
import org.infosec.lc.exercises.ObjectNetworkTransmission.code.objectStream.ObjectStreamUDPReceive;
import org.infosec.lc.exercises.ObjectNetworkTransmission.code.objectStream.ObjectStreamUDPSend;
import org.infosec.lc.exercises.ObjectNetworkTransmission.modle.IdentityCard;
import org.infosec.lc.exercises.ObjectNetworkTransmission.modle.Person;

/**
	文件名：ObjectTranMain.java
	作  者：林超
	时  间：2011-7-20上午10:50:25

	功能描述：对象网络传输方法汇总
			1、把Object对象先序列化为ObjectOutputStream,通过UDP发送,待接收到ObjectInputStream反序列化后得到对象Object.
        	2、把Object对象通过Castor或XStream转换为XML后，以UDP的方式在网络中传输，待接收到后反转换即可得到原始对象Object。
	
	pom.xml	:
			Castor	-->		<dependency>
            					<groupId>org.codehaus.castor</groupId>
            					<artifactId>castor-xml</artifactId>
            					<version>1.3.1</version>
        					</dependency>
        	XStream	-->		<dependency>
    							<groupId>xstream</groupId>
    							<artifactId>xstream</artifactId>
    							<version>1.2</version>
							</dependency>
			
 */
public class ObjectTranMain {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		IdentityCard cardId = new IdentityCard();
		cardId.setCardId(612321198611041734L);
		cardId.setAddress("我也不知道！");
		cardId.setBloodType("O型");
		
		Person person = new Person();
		person.setName("张三");
		person.setCardId(cardId);
		
		ObjectStream(person);
		
		Castor(person);
	}
	
	/**
	 * 把Object对象通过Castor或XStream转换为XML后，以UDP的方式在网络中传输，待接收到后反转换即可得到原始对象Object。
	 * @param person
	 */
	private static void Castor(Person person) {
		//创建接收端
		CastorUDPReceive cUdpRec = new CastorUDPReceive();
		Thread t = new Thread(cUdpRec,"CastorReceiveThread");
		t.start();
		
		//创建发送端
		CastorUDPSend cUdpSend = new CastorUDPSend(person);
		cUdpSend.send();
	}
	
	/**
	 * 把Object对象先序列化为ObjectOutputStream,通过UDP发送,待接收到ObjectInputStream反序列化后得到对象Object.
	 * @param person
	 */
	private static void ObjectStream(Person person) {
		//创建接收端
		ObjectStreamUDPReceive osUdpRec = new ObjectStreamUDPReceive();
		Thread t = new Thread(osUdpRec,"ObjectStreamReceiveThread");
		t.start();
		
		//创建发送端
		ObjectStreamUDPSend osUdpSend = new ObjectStreamUDPSend(person);
		osUdpSend.send();
	}

}

